package com.adxon.data;

import com.adxon.MsgException;
import com.adxon.servlet.ServletBase;

import javax.servlet.http.HttpServletRequest;

public class Item extends Attachee{
	public final static String BSK="i";
	@SuppressWarnings("rawtypes")
	public final static Class[] CLS={
			null,Individual.class,null,null,null,	// 0 - 4
			null,Mark.class,null,null,null,			// 5 - 9
			null,null,null,Link.class,null,			// 10 - 14
			null,null,null,null,null,				// 15 - 19
		};

	public static void main(String[] args){
		try{
			Item e;
			e=new Item();
			e.setText("Test");
			e.commit();
			long i=e.getId();
			System.out.println(i);
			e=new Item();
			e.update(i);
			System.out.println(e.getText());
		}catch(MsgException e){
			e.printStackTrace();
		}
	}

	protected long		ph;		// path id
	protected String	pk;		// permalink
	private Item		ref;	// ref
	protected long		rf;		// ref id
	protected byte 		rk;		// rank
	protected long		ta;		// access time
	protected String	xt;		// extra

	public Item(){
		bsk=BSK;
	}
	/*@Override
	protected String allocateId()throws MsgException{
		if(ph==0)
			return super.allocateId();
		return ph+"/"+super.allocateId();
	}*/
	@Override
	protected void commitExt()throws MsgException{
		super.commitExt();
		if(ta==0)
			ta=tm;
		commit("ph",ph);
		commit("pk",pk);
		commit("rf",rf);
		commit("rk",rk);
		commit("ta",ta);
		commit("xt",xt);
	}
	public Item extend()throws MsgException{
		try{
			@SuppressWarnings("rawtypes")
			Class c=kn<CLS.length?CLS[kn]:null;
			if(c==null)
				return this;
			Item r=(Item)c.newInstance();
			r.setAll(this);
			return r;
		}catch(InstantiationException e){
			throw new MsgException(MsgException.INSTANTIATION,null);
		}catch(IllegalAccessException e){
			throw new MsgException(MsgException.INSTANTIATION,null);
		}
	}
	@Override
	public void getAll(HttpServletRequest req){
		super.getAll(req);
		req.setAttribute("r",Long.toString(rc));
	}
	public String getContentType(){
		return "htm";
	}
	public String getExtra(){
		return xt;
	}
	public String getInfoId()throws MsgException{
		return toString();
	}
	public Item getOwnerEx()throws MsgException{
		return getOwner();
	}
	public long getOwnerIdEx(){
		return ow;
	}
	public String getPermaLink(){
		return pk;
	}
	@Override
	public void getPage(HttpServletRequest req)throws MsgException{
		getAll(req);
		req.setAttribute("Page","article.jsp");
	}
	public int getRank(){
		return rk;
	}
	public Item getRef(){
		if(rf!=0 && ref==null)
			for(int i=100;i>0;i--)try{
				ref=((Item)new Item().update(rf)).extend();
			}catch(MsgException e){
				//e.printStackTrace();
				ServletBase.delay(10);	// TODO:
				ref=null;
			}
		return ref;
	}
	public long getRefId(){
		return rf;
	}
	public String getUrl(){
		return "/"+pk+".htm";
	}
	public void setAccess(){
		ta=0;
	}
	public void setAll(Item src)throws MsgException{
		at=src.at;
		ds=src.ds;
		id=src.id;
		kn=src.kn;
		ow=src.ow;
		ph=src.ph;
		pk=src.pk;
		rc=src.rc;
		rf=src.rf;
		ta=src.ta;
		tc=src.tc;
		tg=src.tg;
		tm=src.tm;
		tx=src.tx;
		xt=src.xt;

		att=src.att;
//		ent=src.ent;
		typ=src.typ;
	}
	public void setExtra(String extra){
		xt=extra;
	}
	@Override
	public void setOwner(long owner){
		super.setOwner(owner);
		if(ph==0)
			ph=owner;
	}
	public void setPermaLink(long permaLink) {
		pk=Long.toString(permaLink);
	}
	public void setPermaLink(String permaLink) {
		pk=permaLink;
	}
	public void setRank(int rank){
		rk=(byte)rank;
	}
	public void setRef(long ref){
		rf=ref;
	}
	public void setup()throws MsgException{
		stp="id BIGINT NOT NULL COMMENT 'id',"+
			"at BIGINT NOT NULL COMMENT 'attachment id',"+
			"ds MEDIUMTEXT NULL COMMENT 'description',"+
			"kn INT NOT NULL COMMENT 'kind',"+
			"ow BIGINT NOT NULL COMMENT 'owner id',"+
			"ph BIGINT NOT NULL COMMENT 'path id',"+
			"pk VARCHAR(255)NULL COMMENT 'permalink',"+
			"rc INT NOT NULL COMMENT 'ref count',"+
			"rf BIGINT NOT NULL COMMENT 'ref id',"+
			"rk TINYINT NOT NULL COMMENT 'rank',"+
			"rt TINYINT NOT NULL COMMENT 'rate',"+
			"ta BIGINT NOT NULL COMMENT 'access time',"+
			"tc BIGINT NOT NULL COMMENT 'create time',"+
			"tg TEXT NULL COMMENT 'tags',"+
			"tm BIGINT NOT NULL COMMENT 'modify time',"+
			"tx VARCHAR(255)NULL COMMENT 'text',"+
			"xt MEDIUMTEXT NULL COMMENT 'extra',"+
			"PRIMARY KEY(id),"+
			"UNIQUE(ph,pk),"+
			"INDEX(tm)";
		super.setup();
	}
	public void setRef(Item ref){
		rf=ref.id;
	}
	public Item update(String permalink)throws MsgException{
		Object[] s={permalink};
		return (Item)update("pk=?",s);
	}
	@Override
	public void updateExt()throws MsgException{
		super.updateExt();
		ph=updateLong("ph");
		pk=updateString("pk");
		rf=updateLong("rf");
		rk=updateByte("rk");
		ta=updateLong("ta");
		xt=updateString("xt");
	}
}